package com.boot23.controller;

import com.boot23.entity.Hero;
import com.boot23.mapper.HeroMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author wxd
 * @version 1.0
 */
@Controller
public class HeroController {
    @Autowired
    HeroMapper mapper;
    @RequestMapping("/addHero")
    @ResponseBody
    public String addHero(Hero hero) {
        System.out.println("hero = " + hero);
        mapper.insert(hero);
        return hero.toString();
    }
    @RequestMapping("/deleteHero")
    @ResponseBody
    public String deleteHero(String name) {
        System.out.println("name = " + name);
        mapper.deleteByName(name);
        return "删除成功!";
    }

    @RequestMapping("/updateHero")
    @ResponseBody
    public String updateHero(Hero hero) {
        System.out.println("hero = " + hero);
        mapper.updateById(hero);
        return "修改完成!";
    }

    @RequestMapping("/queryHero")
    @ResponseBody
    public String queryHero(String name, HttpServletResponse response) throws SQLException, IOException {
        List<Hero> list =  mapper.select();
        return list.toString();
    }
}
